package com.varian.service.exception.handler;

import com.varian.tool.contant.ResultConstant;
import com.varian.tool.result.IR;
import com.varian.tool.result.R;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.MethodParameter;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

/**
 * @author ben
 * @since 2024/7/8
 */
public class MethodArgumentTypeMismatchExceptionHandler implements ExceptionHandlerStrategy {
    @Override
    public Class<? extends Exception> getExceptionClass() {
        return MethodArgumentTypeMismatchException.class;
    }

    @Override
    public IR handle(Exception exception, HttpServletResponse response) {
        MethodArgumentTypeMismatchException methodArgumentTypeMismatchException = (MethodArgumentTypeMismatchException) exception;
        MethodParameter parameter = methodArgumentTypeMismatchException.getParameter();
        Class<?> parameterType = parameter.getParameterType();
        Object value = methodArgumentTypeMismatchException.getValue();
        return R.fail(ResultConstant.FAIL_CODE, "{} 参数类型不匹配，参数类型为：{}，参数值为：{}",
                parameter.getParameterName(), parameterType.getName(), value);
    }
}
